/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pop3;

/**
 *
 * @author Alexandre Marchand
 */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Serveur extends Thread {

        /* Time out */
        final static int TIME_OUT = 1;

        /* Port to listen on */
        public final static int TCP_PORT = 110;

        /* server socket */
        private ServerSocket ss;
        
        /* état du serveur */
        private String state;

        /* Constructor */
        public Serveur() throws IOException {
                ss = new ServerSocket(TCP_PORT);
                state = "AUTH";
        }

        /* Our server starts here 
        public static void main(String[] argv) throws IOException {
                new Serveur().start();
        }*/

        /* Run method from Thread */
        public void run() {
                while (true) {
                        try {
                                System.out.println(Serveur.class.getSimpleName() + " waiting for connection on TCP port " + TCP_PORT);
                                Socket s = ss.accept();
                                BufferedReader is = new BufferedReader(new InputStreamReader(s.getInputStream()));
                                PrintWriter os = new PrintWriter(new BufferedWriter (new OutputStreamWriter (s.getOutputStream())),true);
                                //OutputStreamWriter os = new OutputStreamWriter(s.getOutputStream());
                                os.println("+OK Server ready Bienvenue sur le serveur POP3 qui dechire");
                                //os.flush();
                                
                                String name = is.readLine();
                                os.println("+OK");
                                String passwd = is.readLine();
                                os.println("+OK");
                                //String domain = is.readLine();
                                PrintWriter pout = new PrintWriter(s.getOutputStream(), true);
                                //pout.println("Welcome to " + domain + ", " + name);
                                
                        } catch (IOException e) {
                                System.err.println("Doh! " + e);
                        }
                }
        }
}
